package com.houtai.educenter.controller;


import com.houtai.commonutils.JwtUtils;
import com.houtai.commonutils.Result;
import com.houtai.commonutils.ordersvo.UcenterMemberOrder;
import com.houtai.educenter.entity.UcenterMember;
import com.houtai.educenter.entity.vo.RegisterVo;
import com.houtai.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xst
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录接口
     * @param member
     * @return
     */
    @PostMapping("login")
    public Result loginUser(@Valid @RequestBody UcenterMember member){
        //调用service接口实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return Result.ok().data("token",token);
    }

    /**
     * 注册的
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public Result registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return Result.ok();
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return Result.ok().data("userInfo",member);
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@Valid @PathVariable String id){
        UcenterMember member = memberService.getById(id);
        //把member对象里面的值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("getRegister/{day}")
    public Result getRegister(@PathVariable String day){
        Integer count= memberService.getRegisterDay(day);
        return Result.ok().data("countRegister",count);

    }

}

